package com.ruoyi.gxs.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gxs.domain.GxsComprehensive;
import com.ruoyi.gxs.mapper.GxsComprehensiveMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gxs.mapper.GxsStudyChangeMapper;
import com.ruoyi.gxs.domain.GxsStudyChange;
import com.ruoyi.gxs.service.IGxsStudyChangeService;

/**
 * 学习成绩变化Service业务层处理
 * 
 * @author 高晓松
 * @date 2024-01-03
 */
@Service
public class GxsStudyChangeServiceImpl implements IGxsStudyChangeService 
{
    @Autowired
    private GxsStudyChangeMapper gxsStudyChangeMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private GxsComprehensiveMapper comprehensiveMapper;

    /**
     * 查询学习成绩变化
     * 
     * @param gxsId 学习成绩变化主键
     * @return 学习成绩变化
     */
    @Override
    public GxsStudyChange selectGxsStudyChangeByGxsId(Long gxsId)
    {
        return gxsStudyChangeMapper.selectGxsStudyChangeByGxsId(gxsId);
    }

    /**
     * 查询学习成绩变化列表
     * 
     * @param gxsStudyChange 学习成绩变化
     * @return 学习成绩变化
     */
    @Override
    public List<GxsStudyChange> selectGxsStudyChangeList(GxsStudyChange gxsStudyChange)
    {
        return gxsStudyChangeMapper.selectGxsStudyChangeList(gxsStudyChange);
    }

    /**
     * 新增学习成绩变化
     * 
     * @param gxsStudyChange 学习成绩变化
     * @return 结果
     */
    @Override
    public int insertGxsStudyChange(GxsStudyChange gxsStudyChange)
    {
//        根据学号查询 昵称、用户ID、部门 并添加到gxsStudyChange
        SysUser sysUser = sysUserMapper.selectUserByUserName(gxsStudyChange.getUserName());
        gxsStudyChange.setNickName(sysUser.getNickName());
        gxsStudyChange.setUserId(sysUser.getUserId());
        gxsStudyChange.setDeptId(sysUser.getDeptId());

        //根据新增的专业分数信息 查询综合得分学习成绩
        GxsComprehensive comprehensive = comprehensiveMapper.selectGxsComprehensiveByUserName(gxsStudyChange.getUserName());
        gxsStudyChange.setGxsSchoolrecord(comprehensive.getGxsSchoolrecord());


        //根据加分状态对学习成绩进行加减
        long totalPoints =0L;
        if (gxsStudyChange.getGxsBonusState() == 0L){
            totalPoints = gxsStudyChange.getGxsSchoolrecord() + gxsStudyChange.getGxsScore();
        }
        else {
            totalPoints = gxsStudyChange.getGxsSchoolrecord() - gxsStudyChange.getGxsScore();
        }
        gxsStudyChange.setGxsTotalPoints(totalPoints);
        comprehensive.setGxsSchoolrecord(totalPoints);
        comprehensiveMapper.updateGxsComprehensive(comprehensive);

        gxsStudyChange.setCreateTime(DateUtils.getNowDate());
        return gxsStudyChangeMapper.insertGxsStudyChange(gxsStudyChange);
    }

    /**
     * 修改学习成绩变化
     * 
     * @param gxsStudyChange 学习成绩变化
     * @return 结果
     */
    @Override
    public int updateGxsStudyChange(GxsStudyChange gxsStudyChange)
    {
        gxsStudyChange.setUpdateTime(DateUtils.getNowDate());
        return gxsStudyChangeMapper.updateGxsStudyChange(gxsStudyChange);
    }

    /**
     * 批量删除学习成绩变化
     * 
     * @param gxsIds 需要删除的学习成绩变化主键
     * @return 结果
     */
    @Override
    public int deleteGxsStudyChangeByGxsIds(Long[] gxsIds)
    {
        return gxsStudyChangeMapper.deleteGxsStudyChangeByGxsIds(gxsIds);
    }

    /**
     * 删除学习成绩变化信息
     * 
     * @param gxsId 学习成绩变化主键
     * @return 结果
     */
    @Override
    public int deleteGxsStudyChangeByGxsId(Long gxsId)
    {
        return gxsStudyChangeMapper.deleteGxsStudyChangeByGxsId(gxsId);
    }
}
